package com.lanyou.esb.cook.proxy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;

import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.entity.Token;

/**
 * 接口令牌持久层
 * 
 * @author Davey.wu
 */
public class TokenDao extends BaseDao<Token> {

	private RowMapper<Token> mapper = new RowMapper<Token>() {
		public Token mapRow(ResultSet rs, int rowNum) throws SQLException {
			Token token = new Token();
			token.setId(rs.getString("IF_TOKEN_ID"));
			token.setIfId(rs.getString("IF_ID"));
			token.setIfType(rs.getString("IF_TYPE"));
			token.setIfName(rs.getString("IF_NAME"));
			token.setIfCode(rs.getString("IF_CODE"));
			token.setIfVersion(rs.getString("IF_VERSION"));
			token.setAppKey(rs.getString("APP_KEY"));
			token.setKeyInvalidDate(rs.getDate("KEY_INVALID_DATE"));
			token.setPubSystemId(rs.getString("IF_PUB_SYSTEM_ID"));
			token.setPubSystemCode(rs.getString("IF_PUB_SYSTEM_CODE"));
			token.setCallSystemId(rs.getString("IF_CALL_SYSTEM_ID"));
			token.setCallSytemCode(rs.getString("IF_CALL_SYSTEM_CODE"));
			token.setLinkTel(rs.getString("LINK_TEL"));
			token.setApplyDate(rs.getDate("AK_APPLY_DATE"));
			token.setInvalidTimes(rs.getInt("INVALID_TIMES"));
			token.setApplyUserCode(rs.getString("APPLY_USER_CODE"));
			token.setAccessToken(rs.getString("ACCESS_TOKEN"));
			token.setIpWhiteList(rs.getString("IP_WHITELIST"));
			token.setIsEnable(rs.getString("IS_ENABLE"));
			token.setCreator(rs.getString("CREATOR"));
			token.setCreatedDate(rs.getDate("CREATED_DATE"));
			token.setModifier(rs.getString("MODIFIER"));
			token.setLastUpdatedDate(rs.getDate("LAST_UPDATED_DATE"));
			token.setCurApplyQty(rs.getInt("CUR_APPLY_QTY"));
			token.setStatus(rs.getString("IF_TOKEN_STATUS"));
			return token;
		}
	};

	/**
	 * 保存令牌信息，不会增加新记录，而只是更新记录
	 */
	@Override
	public int save(Token target) {
		return update(target);

	}

	@Override
	public List<Token> findAll() {
		String sql = "select * from T_ESB_BU_IF_TOKEN";
		List<Token> records = this.getJdbcTemplate().query(sql, mapper);
		return records;
	}

	@Override
	public Token find(Map<String, Object> keys) {
		String systemCode = (String) keys.get(Constants.SYSTEM_CODE);
		String ifCode = (String) keys.get(Constants.INTERFACE_CODE);
		String ifVersion = (String) keys.get(Constants.INTERFACE_VERSION);
		String callSystemCode = (String) keys.get(Constants.CALL_SYSTEM_CODE);
		return getToken(systemCode, ifCode, ifVersion, callSystemCode);
	}

	@Override
	public int update(Token token) {
		String sql = "update T_ESB_BU_IF_TOKEN set ACCESS_TOKEN=?, INVALID_TIMES=?, AK_APPLY_DATE=?, CUR_APPLY_QTY=? where APP_KEY=? and IF_CODE=?";
		try {
			return this.getJdbcTemplate().update(sql, token.getAccessToken(),
					token.getInvalidTimes(),
					token.getApplyDate(),
					token.getCurApplyQty(), token.getAppKey(),
					token.getIfCode());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public Token find(Token token) {
		// 发部方系统编码，接口编码，接口版本，调用方系统编码
		if (token == null || StringUtils.isBlank(token.getPubSystemCode())
				|| StringUtils.isBlank(token.getIfCode())
				|| StringUtils.isBlank(token.getIfVersion())
				|| StringUtils.isBlank(token.getCallSytemCode())) {
			return null;
		}
		return getToken(token.getPubSystemCode(), token.getIfCode(),
				token.getIfVersion(), token.getCallSytemCode());
	}

	/**
	 * 根据发部方系统编码，接口编码，接口版本，调用方系统编码查找令牌信息
	 * 
	 * @param systemCode
	 * @param interfaceCode
	 * @param interfaceVersion
	 * @param callSystemCode
	 * @return
	 */
	private Token getToken(String systemCode, String interfaceCode,
			String interfaceVersion, String callSystemCode) {
		// 发部方系统编码，接口编码，接口版本，调用方系统编码不能为空
		if (StringUtils.isBlank(systemCode)
				|| StringUtils.isBlank(interfaceCode)
				|| StringUtils.isBlank(interfaceVersion)
				|| StringUtils.isBlank(callSystemCode)) {
			return null;
		}
		String sql = "select * from T_ESB_BU_IF_TOKEN where IF_PUB_SYSTEM_ID=? and IF_CODE=? and IF_VERSION=? and IF_CALL_SYSTEM_CODE=?";
		try {
			return this.getJdbcTemplate()
					.queryForObject(sql, mapper, systemCode, interfaceCode,
							interfaceVersion, callSystemCode);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return null;
	}
}
